<template>
  <div>
    <el-form :model="addData" ref="addform" label-width="60px" size="small">
      <el-form-item label="分组">
        <el-select v-model="addData.category_id" placeholder="请选择">
          <el-option
            v-for="item in helpList"
            :key="item.id"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="标题">
        <el-input v-model="addData.title" placeholder="请输入" style="width: 80%"/>
      </el-form-item>
      <el-form-item label="内容">
        <tinymce v-model="addData.content" :height="300" width="100%" />
      </el-form-item>
      <el-form-item label="状态">
        <el-switch v-model="addData.status" active-color="#13ce66" active-value="1" inactive-value="0"></el-switch>
      </el-form-item>
      <el-form-item label="序号">
        <el-input v-model="addData.sort" placeholder="请输入" style="width: 80%"/>
      </el-form-item>
    </el-form>

    <div style="text-align:center">
      <el-button type="primary" size="small" style="padding:8px 30px;" @click="submit">提 交</el-button>
    </div>
  </div>
</template>

<script>
import { getGroupList, postHelp, putHelp } from '@/api/help'
import Tinymce from '@/components/Tinymce'

export default {
  components: {
    Tinymce
  },
  props: {
    addData: {
      type: Object,
      required: true
    },
    isAdd: {
      type: Boolean,
      required: true
    }
  },
  data() {
    return {
      helpList: []
    }
  },
  mounted() {
    this.getGroupList();
  },
  methods: {
    /**
     * 提交
     */
    submit() {
      this.$refs.addform.validate((valid) => {
        if(valid) {
          if(this.isAdd){
            postHelp(this.addData).then(response => {
              if(response.status === 200){
                this.$emit('success');
                this.$message.success('添加成功！');
              }else{
                this.$message.error(response.message);
              }
            })
          }else{
            putHelp(this.addData).then(response => {
              if(response.status === 200){
                this.$emit('success');
                this.$message.success('修改成功！');
              }else{
                this.$message.error(response.message);
              }
            })
          }
        }
      })    
    },
    /**
     * 获取积分商品列表
     */
    getGroupList() {
      const params = {
        key: this.$store.state.app.activeApp.saa_key,
      };
      getGroupList(params).then(res => {
        if(res.status === 200){
          this.helpList = res.data;
          this.addData.category_id = this.helpList[0].id;
        }else if(res.status === 204){
          this.$message.warning('未检索到问题分组，请先创建问题分组');
        }else{
          this.$message.error(res.message);
        }
      })
    },
  },
}
</script>